/**
 * 
 */
package com.srini.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.srini.pojos.LoggedInUser;
import com.srini.pojos.PaidDayOff;
import com.srini.util.AppUtil;
import com.srini.util.ConnectionUtil;
import com.srini.util.Constants;
import com.srini.util.JDBCDao;
import com.vssr.persist.DataBaseWriter;

/**
 * @author Sriniz
 * 
 */
public class AddHolidayServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		LoggedInUser user = (LoggedInUser) req.getSession().getAttribute(
				Constants.LOGGEDIN_USER);
		String date = req.getParameter("holiday");
		String type = req.getParameter("type");
		String leaves = req.getParameter("leaves");
		String desc = req.getParameter("desc");
		String leaveTypes = req.getParameter("types");

		String message = "", currentPage = "";
		String[] dates = null, types = null;
		PaidDayOff off = null;
		if (leaves != null) {
			int withDrawlCount = 0;
			dates = leaves.split(",");
			types = leaveTypes.split(",");
			resp.setContentType("application/html");
			if (dates != null) {
				for (int i = 0; i < types.length; i++) {
					if (types[i].equalsIgnoreCase("new")) {
						off = new PaidDayOff();
						off.setDayOffType(type);
						off.setPaidDayOff(dates[i]);
						off.setUserId(user.getId());

						DataBaseWriter.save(off);
						message = "Leave(s) successfully applied.";
					} else if (types[i].equalsIgnoreCase("deleted")) {
						String[] whrColNames = { "userId", "date" };
						Object[] whrColVals = { Integer.valueOf(user.getId()),
								dates[i] };
						JDBCDao.deleteRecord("leaves", whrColNames, whrColVals,
								new ConnectionUtil().getConnection());
						withDrawlCount++;
					}
				}

				if (withDrawlCount > 0) {
					if (!AppUtil.isEmpty(message)) {
						message += "<br>";
					}
					message += withDrawlCount + " Leave(s) widthdrawn";
				}
				PrintWriter writer = resp.getWriter();
				writer.print(message);
			}
		} else {

			off = new PaidDayOff();
			off.setDayOffType(type);
			off.setPaidDayOff(date);
			off.setUserId(user.getId());
			off.setYear(date.substring(date.lastIndexOf("/") + 1));
			off.setDescription(desc);
			DataBaseWriter.save(off);
			message = "Holiday successfully added.";
			currentPage = "compset";
			req.setAttribute("message", message);
			req.setAttribute("currentPage", currentPage);
			req.getRequestDispatcher("/home.jsp").forward(req, resp);
		}

	}
}
